/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ssn.main.web;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import ssn.common.util.DateUtil;
import ssn.common.vo.WaterConstants;
import ssn.common.web.CommonController;
import ssn.main.service.MainService;
import ssn.main.vo.MainVO;
import egovframework.rte.fdl.cmmn.exception.EgovBizException;
import egovframework.rte.fdl.string.EgovStringUtil;


/**  
 * @Class Name : 
 * @Description : 
 * @Modification Information  
 * @
 * @  수정일      수정자              수정내용
 * @ ---------   ---------   -------------------------------
 * @ 2009.03.16           최초생성
 * 
 * @since 2013. 09.30
 * @version 1.0
 * @see
 */
@SuppressWarnings("rawtypes")
@Controller
public class MainController extends CommonController{
	
    @Resource(name = "mainService")
    private MainService mainService;
	
    /**
	 * 메인 페이지 
	 * @param 
	 * @param model
	 * @return 
	 * @exception Exception
	 */
    @RequestMapping(value="/main.do")
    public String main(@ModelAttribute("mainVO") MainVO mainVO, ModelMap model) throws Exception {
    	String returnView = "/main/main";
    	Map resultMap = null;
    	if(EgovStringUtil.isEmpty(mainVO.getSearchEdate())){
			mainVO.setSearchEdate(DateUtil.getCDAsStringYYYY_MM_DD());
		}
		if(EgovStringUtil.isEmpty(mainVO.getSearchSdate())){
			mainVO.setSearchSdate(DateUtil.getCalcDateAsStringYyyyMmDd(mainVO.getSearchEdate(), WaterConstants.SEARCH_DEFAULT_LAST_MONTH, "month"));
		}
    	if(loginSessionInfo.get().isAdmin()){
    		returnView = "/main/admin";
    		resultMap = mainService.selectAdminMainInfo(mainVO);
    		model.addAttribute("zoneList", resultMap.get("zoneList"));
    		model.addAttribute("chartInfoList", resultMap.get("chartInfoList"));
    	}else{
    		mainVO.setArea_key(loginSessionInfo.get().getSsa_area_key());
    		mainVO.setSearchServiceArea(loginSessionInfo.get().getSsa_area_key());
    		resultMap = mainService.selectMainInfo(mainVO);
    		model.addAttribute("unusualYn", resultMap.get("unusualYn"));
    		model.addAttribute("unusualCont", resultMap.get("unusualCont"));
    		model.addAttribute("chartInfo", resultMap.get("chartInfo"));
    	}
    	model.addAttribute("mainVO", mainVO);
    	model.addAttribute("mainInfo", resultMap.get("mainInfo"));
        return returnView;
    }
    
    /**
     * 메인 페이지 전체 차트 보기
     * @param 
     * @param model
     * @return 
     * @exception Exception
     */
    @RequestMapping(value="/mainChartAll.do")
    public String mainChartAll(@ModelAttribute("mainVO") MainVO mainVO, ModelMap model) throws Exception {
    	if(!loginSessionInfo.get().isAdmin()){
    		throw new EgovBizException(propertiesService.getString("info.grade.msg"));
    	}
    	if(EgovStringUtil.isEmpty(mainVO.getSearchEdate())){
			mainVO.setSearchEdate(DateUtil.getCDAsStringYYYY_MM_DD());
		}
		if(EgovStringUtil.isEmpty(mainVO.getSearchSdate())){
			mainVO.setSearchSdate(DateUtil.getCalcDateAsStringYyyyMmDd(mainVO.getSearchEdate(), WaterConstants.SEARCH_DEFAULT_LAST_MONTH, "month"));
		}
		Map resultMap = mainService.selectAdminChartAll(mainVO);
		model.addAttribute("zoneList", resultMap.get("zoneList"));
		model.addAttribute("chartInfoList", resultMap.get("chartInfoList"));
    	model.addAttribute("mainVO", mainVO);
    	return "/main/chartAll";
    }
}
